package com.waitingsnow.modules.shell.view.baseView
{
import assets.shellView.PlayBottomAsset;

import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.model.vo.GiftVO;
import com.waitingsnow.core.util.ds.ExternalUtil;
import com.waitingsnow.modules.shell.model.vo.RubyServerVO;
import com.waitingsnow.modules.shell.view.giftItem.BaseGiftItem;
import com.waitingsnow.modules.shell.view.giftItem.SpecialGiftItem;

import fl.controls.ComboBox;
import fl.controls.List;
import fl.controls.TileList;
import fl.data.DataProvider;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;

/**
 * 
 * @author waitingsnow
 * $Id: PlayBottomView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class PlayBottomView extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>PlayBottomView</code> instance.
	 * 
	 */
	public function PlayBottomView()
	{
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var playBottomAsset:PlayBottomAsset;
	private var rubyServerVO:RubyServerVO;
	private var giftNum_cb:ComboBox;
	private var giftNum_tf:TextField;
	private var gift_sp:Sprite;
	private var speciaGiftItem_sp:SpecialGiftItem;
	private var curGiftVO:GiftVO;
	
	private var giftCbArr:Array = [{label:"1", data:1},
		{label:"10", data:10},
		{label:"50", data:50},
		{label:"100", data:100},
		{label:"500", data:500},
		{label:"1000", data:1000}];
	
	//==========================================================================
	//  Methods
	//==========================================================================
	
	public function setGiftData(vo:RubyServerVO):void
	{
		rubyServerVO = vo;
		if (!playBottomAsset)
		{
			playBottomAsset = new PlayBottomAsset();
		}
		playBottomAsset.charge_btn.addEventListener(MouseEvent.CLICK, chargeBtnClickHandler);
		playBottomAsset.sendGift_btn.addEventListener(MouseEvent.CLICK, sendGiftBtnClickHandler);
		updateAudianceValHandler(vo.audienceNum);
		addChild(playBottomAsset);
		initCb();
		initGift();
		initSpecialGift();
		//Externalvars
		ExternalUtil.callback(AppConfig.UPDATE_AUDIANCE_TO_SWF, updateAudianceValHandler);
		ExternalUtil.callback(AppConfig.UPDATE_FIRE_WORKS_TO_SWF, updateFireWorksValHandler);
		ExternalUtil.callback(AppConfig.SEND_GIFT_TO_SWF, sendGiftResultHandler);
		ExternalUtil.callback(AppConfig.FIREWORKS_READY_TO_SWF, fireWorksReadyHandler);
	}
	
	public function resize(stageFlag:Boolean = false):void
	{
		if (playBottomAsset)
		{
			var w:int = (stage && stageFlag) ? stage.stageWidth : AppConfig.swfWidth;
			playBottomAsset.bg_mc.width = w;
			speciaGiftItem_sp && (speciaGiftItem_sp.x = w - 40);
			playBottomAsset.charge_btn.x = w - 290;
			playBottomAsset.audiance_txt.x = w - 230;
			playBottomAsset.fireworksNum_txt.x = w - 160;
		}
	}
	
	public function dispose():void
	{
		mouseEnabled = mouseChildren = false;
	}
	
	private function initCb():void
	{
		giftNum_cb = new ComboBox();
		giftNum_cb.setSize(60, 22);
		giftNum_cb.rowCount = 6;
		giftNum_cb.x = 102;
		giftNum_cb.y = 10;
		giftNum_cb.textField.visible = false;
		giftNum_cb.dataProvider = new DataProvider(giftCbArr);
		giftNum_cb.selectedIndex = 0;
		giftNum_cb.addEventListener(Event.CHANGE, giftNumCbChangeHandler);
		giftNum_cb.mouseChildren = true;
		addChild(giftNum_cb);
		giftNum_tf = new TextField();
		giftNum_tf.restrict = "0-9";
		giftNum_tf.width = 36;
		giftNum_tf.height = 20;
		giftNum_tf.type = TextFieldType.INPUT;
		var tft:TextFormat = new TextFormat();
		tft.color = 0xFFFFFF;
		tft.indent = 4;
		giftNum_tf.defaultTextFormat = tft;
		giftNumCbChangeHandler(null);
		giftNum_tf.addEventListener(Event.CHANGE, giftNumTfChangeHandler);
		giftNum_cb.addChild(giftNum_tf);
	}
	
	private function initGift():void
	{
		gift_sp = new Sprite();
		gift_sp.x = 60;
		gift_sp.y = 10;
		var giftItem:BaseGiftItem;
		var index:int = 0;
		for each (var giftVO:GiftVO in rubyServerVO.giftArr)
		{
			if (giftVO.giftType == 1 && giftVO.isShortCut)
			{
				giftItem = new BaseGiftItem();
				giftItem.setData(giftVO);
				giftItem.x = 32*index;
				/*giftItem.addEventListener(MouseEvent.CLICK, giftItemClickHandler);*/
				gift_sp.addChild(giftItem);
				index ++;
				curGiftVO = giftVO;
			}
		}
		addChild(gift_sp);
	}
	
	private function initSpecialGift():void
	{
		var giftVO:GiftVO;
		rubyServerVO.giftArr.length && (giftVO = rubyServerVO.giftArr[0]);
		if (giftVO)
		{
			speciaGiftItem_sp = new SpecialGiftItem();
			speciaGiftItem_sp.totalTime = ExternalVars.freezeSeconds;
			speciaGiftItem_sp.x = AppConfig.swfWidth - 40;
			speciaGiftItem_sp.y = 8;
			speciaGiftItem_sp.setData(giftVO);
			speciaGiftItem_sp.addEventListener(MouseEvent.CLICK, speciaGiftClickHandler);
			addChild(speciaGiftItem_sp);
		}
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function updateAudianceValHandler(val:int):void
	{
		Logger.log("UPDATE_AUDIANCE_TO_SWF(" + val + ")");
		rubyServerVO.audienceNum = val;
		playBottomAsset.audiance_txt.text = String(val) + "名观众";
	}
	
	private function updateFireWorksValHandler(val:int):void
	{
		Logger.log("UPDATE_FIRE_WORKS_TO_SWF(" + val + ")");
		rubyServerVO.fireWorksNum = val;
		playBottomAsset.fireworksNum_txt.text = "观众献花数：" + val;
	}
	
	private function sendGiftResultHandler(giftID:int, result:int, type:uint, leftTime:int):void
	{
		Logger.log("SEND_GIFT_TO_SWF(" + giftID + "," + result + "," + type + "," + leftTime + ")");
		if (type == 2 && result == 1)
		{
			speciaGiftItem_sp && speciaGiftItem_sp.countDownGift();
		}
	}
	
	private function fireWorksReadyHandler():void
	{
		speciaGiftItem_sp && speciaGiftItem_sp.isEnd();
	}
	
	private function chargeBtnClickHandler(event:MouseEvent):void
	{
		ExternalUtil.call(AppConfig.RECHARGE_TO_JS);
	}
	
	private function sendGiftBtnClickHandler(event:MouseEvent):void
	{
		if (curGiftVO)
		{
			ExternalUtil.call(AppConfig.SEND_GIFT_TO_JS, curGiftVO.giftID, 
				int(giftNum_tf.text));
		}
	}
	
	private function speciaGiftClickHandler(event:MouseEvent):void
	{
		if (speciaGiftItem_sp.isReady)
		{
			var vo:GiftVO = speciaGiftItem_sp.giftVO;
			ExternalUtil.call(AppConfig.SEND_GIFT_TO_JS, vo.giftID, 1);
		}
	}
	
	private function giftNumCbChangeHandler(event:Event):void
	{
		if (giftNum_cb.selectedIndex != -1)
		{
			giftNum_tf.text = giftNum_cb.selectedItem.data;
		}
	}
	
	private function giftNumTfChangeHandler(event:Event):void
	{
		event.stopImmediatePropagation();
		var count:int = int(giftNum_tf.text);
		count = Math.max(1, Math.min(9999, count));
		giftNum_tf.text = String(count);
	}
}
}